/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 62536
 * Date: 2024-06-12
 * Time: 20:57
 */
public class Solution {

    //打家劫舍II
    public int rRob(int[] nums) {
        if(nums.length == 0){
            return 0;
        }
        if(nums.length == 1){
            return nums[0];
        }

        return Math.max(rob1(nums), rob2(nums));
    }

    public int rob1(int[] nums){
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];
        f[1] = nums[1];
        g[1] = 0;

        for (int i = 2; i < n; i++) {
            f[i] = g[i-1] + nums[i];
            g[i] = Math.max(g[i-1], f[i-1]);
        }
        return Math.max(g[n-1],f[n-1]);
    }

    public int rob2(int[] nums) {
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];
        f[0] = nums[0];

        for (int i = 1; i < n-1; i++) {
            f[i] = g[i-1] + nums[i];
            g[i] = Math.max(g[i-1], f[i-1]);
        }
        return Math.max(g[n-2],f[n-2]);
    }


    //打家劫舍
    public int rob(int[] nums) {
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];
        f[0] = nums[0];

        for (int i = 1; i < n; i++) {
            f[i] = g[i-1] + nums[i];
            g[i] = Math.max(g[i-1], f[i-1]);
        }
        return Math.max(g[n-1],f[n-1]);
    }
}
